Sub FetchAndDisplayEmployeeData()
    Dim wsRecord As Worksheet, wsRoster As Worksheet
    Dim employeeNumber As String, inputNumber As String
    Dim lastRow As Long, employeeRow As Long, i As Integer, j As Integer
    Dim headerRange As Range, cell As Range, photoPath As String, pic As Picture

    ' 워크시트 설정
    Set wsRecord = ThisWorkbook.Sheets("인사기록카드")
    Set wsRoster = ThisWorkbook.Sheets("직원명부")

    ' 사용자로부터 사번 입력 받기
    inputNumber = InputBox("직원의 사번을 입력하세요.", "사번 입력")
    If inputNumber = "" Then
        MsgBox "사번이 입력되지 않았습니다.", vbExclamation
        Exit Sub
    End If
    employeeNumber = inputNumber

    ' 직원명부에서 직원 행 찾기
    With wsRoster
        lastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
        For i = 1 To lastRow
            If .Cells(i, 1).Value = employeeNumber Then
                employeeRow = i
                Exit For
            End If
        Next i
    End With

    ' 직원 번호를 찾았다면
    If employeeRow > 0 Then
        ' 컬럼명에 맞게 데이터 가져오기 및 표시
        Set headerRange = wsRecord.Range("A8:A18, C2:C13")  ' 인사기록카드에 데이터를 입력할 범위
        For Each cell In headerRange
            j = GetColumnByHeader(wsRoster, cell.Value)
            If j > 0 Then
                cell.Offset(0, 1).Value = wsRoster.Cells(employeeRow, j).Value
            End If
        Next cell

        ' 사진 표시
        photoPath = wsRoster.Cells(employeeRow, GetColumnByHeader(wsRoster, "사진")).Value
        Set pic = wsRecord.Pictures.Insert(photoPath)
        With pic
            .ShapeRange.LockAspectRatio = msoTrue
            .Width = Application.CentimetersToPoints(3.2)  ' 가로 폭을 3.2cm로 조정
            .Top = wsRecord.Range("B2").Top
            .Left = wsRecord.Range("B2").Left
        End With
    Else
        MsgBox "해당 사번의 직원을 찾을 수 없습니다.", vbCritical
    End If
End Sub

Function GetColumnByHeader(ws As Worksheet, header As String) As Integer
    Dim cell As Range
    For Each cell In ws.Rows(1).Cells ' 헤더가 첫 번째 행에 있다고 가정
        If cell.Value = header Then
            GetColumnByHeader = cell.Column
            Exit Function
        End If
    Next cell
    GetColumnByHeader = 0 ' 헤더를 찾지 못한 경우
End Function